﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using INSTA.StockApplication.BLL;

namespace INSTA.StockApplication.UI.Windows
{
    public partial class Clients : Form
    {
        DataGridViewButtonColumn btnModifier = new DataGridViewButtonColumn();
        DataGridViewButtonColumn btnSupprimer = new DataGridViewButtonColumn();
        Customer c = new Customer();

        public Clients()
        {
            InitializeComponent();

            RefreshDataGrid();
            
            btnModifier.UseColumnTextForButtonValue = true;
            btnSupprimer.UseColumnTextForButtonValue = true;
            btnModifier.Name = "Modifier";
            btnSupprimer.Name = "Supprimer";
            btnModifier.Text = "Modifier";
            btnSupprimer.Text = "Supprimer";
            dataGridView1.Columns.Add(btnModifier);
            dataGridView1.Columns.Add(btnSupprimer);
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //clic btn suppression
            if (e.ColumnIndex == dataGridView1.Columns.IndexOf(btnSupprimer))
            {
                string messageBoxText = "Etes-vous sûr de vouloir supprimer " + dataGridView1.Rows[e.RowIndex].Cells["PRENOM"].Value + "" +
                                        dataGridView1.Rows[e.RowIndex].Cells["NOM"].Value + " ?";

                var result = MessageBox.Show(messageBoxText, "Suppression", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);

                if (result == System.Windows.Forms.DialogResult.Yes)
                {
                    if (c.DeleteCustomerFromDatabase(Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["ID"].Value)))
                    {
                        MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells["PRENOM"].Value + "" +
                                        dataGridView1.Rows[e.RowIndex].Cells["NOM"].Value + " supprimé");
                    }
                    else
                    {
                        MessageBox.Show("Erreur lors de la suppression");
                    }
                }
                                
            }

            //clic btn modifier
            if (e.ColumnIndex == dataGridView1.Columns.IndexOf(btnModifier))
            {
                int id = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["ID"].Value);
                string nom = dataGridView1.Rows[e.RowIndex].Cells["NOM"].Value.ToString();
                string prenom = dataGridView1.Rows[e.RowIndex].Cells["PRENOM"].Value.ToString();
                string mail = dataGridView1.Rows[e.RowIndex].Cells["MAIL"].Value.ToString();
                string adresse = dataGridView1.Rows[e.RowIndex].Cells["ADRESSE"].Value.ToString();
                string cp = dataGridView1.Rows[e.RowIndex].Cells["CP"].Value.ToString();
                string ville = dataGridView1.Rows[e.RowIndex].Cells["VILLE"].Value.ToString();
                string pays = dataGridView1.Rows[e.RowIndex].Cells["PAYS"].Value.ToString();
                string telephone = dataGridView1.Rows[e.RowIndex].Cells["TELEPHONE"].Value.ToString();
                Form fen = new AddUser(id,nom,prenom,mail,adresse,cp,ville,pays,telephone);
                fen.Show();
            }
            RefreshDataGrid();
        }

        private void RefreshDataGrid()
        {
            dataGridView1.DataSource = c.GetListOfUsersFromDatabase();
        }


    }
}
